package fenetres;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;

import controles.*;
import database.DBManager;


public class ChoiceSupplierFrame extends JInternalFrame implements ActionListener{
	JPanel fondPanel;
	JComboBox<String> nomFrsCombo;
	JTextField codeTxt;
	JButton choiceBut,cancelBut;
	Vector<Fournisseur> listFrs;
	Utilisateur userConnected;
	Accueil oAccueil;
	
	public ChoiceSupplierFrame(Utilisateur _userConnected,Accueil _oAccueil){
		super("Choix du fournisseur",
	              false, //resizable
	              true, //closable
	              false, //maximizable
	              false);//iconifiable
		userConnected = _userConnected;
		oAccueil = _oAccueil;
		boolean accept = checkUserConnected();
		if (accept){
			fondPanel = new JPanel();
			etablishPanel();
			this.getContentPane().setLayout(null);
			this.getContentPane().add(fondPanel);
			//this.setBounds(500,150,300,200);
			setLocation(60, 60);
			this.setSize(400,200);
			this.setTitle("Choix Fournisseur");
			this.setVisible(true);
			this.setResizable(false);
		}
	}
	
	public boolean checkUserConnected(){
		//userConnected = accueil.authent.getUserConnected();
		if( userConnected == null){
			JOptionPane.showMessageDialog(null, "Aucun utilisateur connecte!! \n Veuillez connecter svp :) ","Erreur",JOptionPane.ERROR_MESSAGE);
			this.dispose();
			return false;
		}
		else {
			return true;
		}
	}

	public void etablishPanel(){
		fondPanel.setLayout(null);
		fondPanel.setBounds(0,0,400,200);
		
		//
		 DBManager dbManager = new DBManager ();
		 listFrs = dbManager.getListFournisseur();
		 String [] listFrsTab = new String[listFrs.size()];
		 for (int iIndex = 0 ; iIndex < listFrs.size(); iIndex++)
		{
			 Fournisseur oFournisseur = listFrs.get(iIndex);
			 listFrsTab [iIndex] = oFournisseur.getFourn_nom();
		}
		 nomFrsCombo = new JComboBox (listFrsTab);
		// nomFrsCombo.setSelectedIndex(0);
		 nomFrsCombo.addActionListener(this);
		 
		JLabel nomLab = new JLabel("Nom: ");
		JLabel codeLab =  new JLabel("Code: ");
		codeTxt = new JTextField();
		//initialisation
		/*Fournisseur oFrs = listFrs.get(0);
		codeTxt.setText(oFrs.getFourn_code());*/
		
		//Bouttons
		choiceBut = new JButton("Choisir");
		choiceBut.addActionListener(this);
		cancelBut = new JButton ("Annuler");
		cancelBut.addActionListener(this);
		
		fondPanel.add(nomLab);
		fondPanel.add(nomFrsCombo);
		fondPanel.add(codeLab);
		fondPanel.add(codeTxt);
		fondPanel.add(choiceBut);
		fondPanel.add(cancelBut);
		
		//position des objets
		nomLab.setBounds(5,5,100,30);
		nomFrsCombo.setBounds(120,5,220,30);
		codeLab.setBounds(5,50,100,30);
		codeTxt.setBounds(120,50,120,30);
		choiceBut.setBounds(60,120,120,30);
		cancelBut.setBounds(210,120,120,30);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getSource() == nomFrsCombo){
			int indiceSelected = nomFrsCombo.getSelectedIndex();
			System.out.println("ici "+indiceSelected);
			Fournisseur oFrs = listFrs.get(indiceSelected);
			codeTxt.setText(oFrs.getFourn_code());
			
		}
		if(arg0.getSource() == cancelBut){
			this.dispose();
		}
		if(arg0.getSource() == choiceBut){
			int indiceSelected = nomFrsCombo.getSelectedIndex();
			Fournisseur oFournisseur = listFrs.get(indiceSelected);
			
			
			UpdateSupplierFrame updateSupplierFrame = new UpdateSupplierFrame(userConnected,oFournisseur);
			updateSupplierFrame.setVisible(true); 
	        oAccueil.desktop.add(updateSupplierFrame);
	        try {
	        	updateSupplierFrame.setSelected(true);
	        } catch (java.beans.PropertyVetoException e) {}
	        
	        this.dispose();
		}
		
		
		
	}
}
